home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / info-service / www / src / WWW / Library / Implementation / HTString.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-21  |  3.1 KB  |  134 lines

  1. /*        Case-independent string comparison        HTString.c
  2. **
  3. **    Original version came with listserv implementation.
  4. **    Version TBL Oct 91 replaces one which modified the strings.
  5. **    02-Dec-91 (JFG) Added stralloccopy and stralloccat
  6. **    23 Jan 92 (TBL) Changed strallocc* to 8 char HTSAC* for VM and suchlike
  7. **     6 Oct 92 (TBL) Moved WWW_TraceFlag in here to be in library
  8. */
  9. #include <ctype.h>
  10. #include "HTUtils.h"
  11. #include "tcp.h"
  12.  
  13. PUBLIC int WWW_TraceFlag = 0;    /* Global trace flag for ALL W3 code */
  14.  
  15. #ifndef VC
  16. #define VC "unknown"
  17. #endif
  18.  
  19. PUBLIC CONST char * HTLibraryVersion = VC; /* String for help screen etc */
  20.  
  21. #ifndef VM        /* VM has these already it seems */
  22.     
  23. /*    Strings of any length
  24. **    ---------------------
  25. */
  26. PUBLIC int strcasecomp ARGS2 (CONST char*,a, CONST char *,b)
  27. {
  28.     CONST char *p =a;
  29.     CONST char *q =b;
  30.     for(p=a, q=b; *p && *q; p++, q++) {
  31.         int diff = TOLOWER(*p) - TOLOWER(*q);
  32.         if (diff) return diff;
  33.     }
  34.     if (*p) return 1;    /* p was longer than q */
  35.     if (*q) return -1;    /* p was shorter than q */
  36.     return 0;        /* Exact match */
  37. }
  38.  
  39.  
  40. /*    With count limit
  41. **    ----------------
  42. */
  43. PUBLIC int strncasecomp ARGS3(CONST char*,a, CONST char *,b, int,n)
  44. {
  45.     CONST char *p =a;
  46.     CONST char *q =b;
  47.     
  48.     for(p=a, q=b;; p++, q++) {
  49.         int diff;
  50.         if (p == a+n) return 0;    /*   Match up to n characters */
  51.         if (!(*p && *q)) return *p - *q;
  52.         diff = TOLOWER(*p) - TOLOWER(*q);
  53.         if (diff) return diff;
  54.     }
  55.     /*NOTREACHED*/
  56. }
  57. #endif
  58.  
  59. /*    Allocate a new copy of a string, and returns it
  60. */
  61. PUBLIC char * HTSACopy
  62.   ARGS2 (char **,dest, CONST char *,src)
  63. {
  64.   if (*dest) free(*dest);
  65.   if (! src)
  66.     *dest = NULL;
  67.   else {
  68.     *dest = (char *) malloc (strlen(src) + 1);
  69.     if (*dest == NULL) outofmem(__FILE__, "HTSACopy");
  70.     strcpy (*dest, src);
  71.   }
  72.   return *dest;
  73. }
  74.  
  75. /*    String Allocate and Concatenate
  76. */
  77. PUBLIC char * HTSACat
  78.   ARGS2 (char **,dest, CONST char *,src)
  79. {
  80.   if (src && *src) {
  81.     if (*dest) {
  82.       int length = strlen (*dest);
  83.       *dest = (char *) realloc (*dest, length + strlen(src) + 1);
  84.       if (*dest == NULL) outofmem(__FILE__, "HTSACat");
  85.       strcpy (*dest + length, src);
  86.     } else {
  87.       *dest = (char *) malloc (strlen(src) + 1);
  88.       if (*dest == NULL) outofmem(__FILE__, "HTSACat");
  89.       strcpy (*dest, src);
  90.     }
  91.   }
  92.   return *dest;
  93. }
  94.  
  95.  
  96. /*    Find next Field
  97. **    ---------------
  98. **
  99. ** On entry,
  100. **    *pstr    points to a string containig white space separated
  101. **        field, optionlly quoted.
  102. **
  103. ** On exit,
  104. **    *pstr    has been moved to the first delimiter past the
  105. **        field
  106. **        THE STRING HAS BEEN MUTILATED by a 0 terminator
  107. **
  108. **    returns    a pointer to the first field
  109. */
  110. PUBLIC char * HTNextField ARGS1(char **, pstr)
  111. {
  112.     char * p = *pstr;
  113.     char * start;            /* start of field */
  114.     
  115.     while(*p && WHITE(*p)) p++;        /* Strip white space */
  116.     if (!*p) {
  117.     *pstr = p;
  118.         return NULL;        /* No first field */
  119.     }
  120.     if (*p == '"') {            /* quoted field */
  121.         p++;
  122.     start = p;
  123.     for(;*p && *p!='"'; p++) {
  124.         if (*p == '\\' && p[1]) p++;    /* Skip escaped chars */
  125.     }
  126.     } else {
  127.     start = p;
  128.     while(*p && !WHITE(*p)) p++;    /* Skip first field */
  129.     }
  130.     if (*p) *p++ = 0;
  131.     *pstr = p;
  132.     return start;
  133. }
  134.